package com.byx.chatclient.view.activity.user;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TableRow;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.byx.chatclient.R;
import com.byx.chatclient.adapter.LoginDropDownAdapter;
import com.byx.chatclient.bean.LoginDropDownBean;
import com.byx.chatclient.contract.user.LoginTaskContract;
import com.byx.chatclient.pojo.LoginVo;
import com.byx.chatclient.presenter.user.LoginPresenter;
import com.byx.chatclient.view.activity.BaseActivity;
import com.byx.chatclient.view.activity.main.MainActivity;
import com.byx.passwordedittext.PasswordEditText;
import com.byx.spinneredittext.DropdownEditText;
import com.gyf.immersionbar.ImmersionBar;
import com.syd.oden.circleprogressdialog.core.CircleProgressDialog;

import java.util.List;

import butterknife.BindView;
import soup.neumorphism.NeumorphButton;

/**
 * @author Superb
 * 登录页面
 */
public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginTaskContract.View {
    private static final String TAG = LoginActivity.class.getSimpleName();

    @BindView(R.id.anim_btn_login)
    protected NeumorphButton mBtnAnimLogin;
    @BindView(R.id.ll_account)
    protected TableRow mTrAccount;
    @BindView(R.id.dropdown_et_account)
    protected DropdownEditText mDropDownEditText;
    @BindView(R.id.pwd_et_pwd)
    protected PasswordEditText mPasswordEditText;
    @BindView(R.id.tv_forget_pwd)
    protected TextView mTvForgetPwd;
    @BindView(R.id.tv_register_account)
    protected TextView mTvRegister;

    private Long mUserInfoLastModifiedTime;

    private CircleProgressDialog circleProgressDialog;

    @Override
    protected void setRootView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_login);
    }


    @Nullable
    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @Override
    protected void initialize() {
        ImmersionBar.with(this).transparentStatusBar().statusBarDarkFont(true).init();
    }

    @Override
    protected void setListeners() {
        /* 点击登录 */
        mBtnAnimLogin.setOnClickListener(v -> {
            presenter.login();
        });

//        //示例数据
//        List<LoginDropDownBean> loginVoList =new ArrayList<>();
//        LoginDropDownBean bean = new LoginDropDownBean();
//        bean.setAvatar(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_app_logo_xxxhdpi));
//        bean.setLoginId("10000");
//        bean.setPassword("12345678a");
//        LoginDropDownBean bean2 = new LoginDropDownBean();
//        bean2.setAvatar(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_app_logo_xxxhdpi));
//        bean2.setLoginId("10001");
//        bean2.setPassword("12345678a");
//        loginVoList.add(bean);
//        loginVoList.add(bean2);

        List<LoginDropDownBean> loginInfo = presenter.getLoginInfo();
        if (loginInfo.isEmpty()){
            mDropDownEditText.getDropDownButton().setVisibility(View.GONE);
        }

        final LoginDropDownAdapter loginDropDownAdapter =new LoginDropDownAdapter(this, loginInfo);
        loginDropDownAdapter.setOnDeleteListener(position -> {
            LoginDropDownBean loginDropDownBean = (LoginDropDownBean) loginDropDownAdapter.getItem(position);
            presenter.deleteByLoginId(loginDropDownBean.getLoginId());
            loginDropDownAdapter.removeItem(position);
        });
        mDropDownEditText.setAdapter(loginDropDownAdapter);
        mDropDownEditText.setAnchor(mTrAccount);
        mDropDownEditText.setPopupWindowBackgroundDrawable(ContextCompat.getDrawable(this,R.drawable.login_activity_table_row));
        mDropDownEditText.setOnItemClickListener((parent, view, position, id) -> {
            LoginDropDownBean loginDropDownBean = (LoginDropDownBean) loginDropDownAdapter.getItem(position);
            mDropDownEditText.setText(loginDropDownBean.getLoginId());
            mPasswordEditText.setText(loginDropDownBean.getPassword());
            mPasswordEditText.setSafeMode(true);
        });

        mTvForgetPwd.setOnClickListener(v -> {
            startActivity(ForgetPwdActivity.class);
        });

        mTvRegister.setOnClickListener(v -> {
            startActivity(RegisterActivity.class);
        });
    }

    /**
     * 是否支持滑动返回。这里在父类中默认返回 true 来支持滑动返回，如果某个界面不想支持滑动返回则重写该方法返回 false 即可
     *
     * @return
     */
    @Override
    public boolean isSupportSwipeBack() {
        return true;
    }

    @Override
    public void onError(String errorMsg, int code) {
        toastQuick(errorMsg);
        Log.d(TAG, "onError: 登录失败"+errorMsg+",错误代码："+code);
    }

    @Override
    public void onError(String errorMsg) {
        toastQuick(errorMsg);
        Log.d(TAG, "onError: 登录失败"+errorMsg);
    }

    @Override
    public void onSuccess() {
        Log.d(TAG, "onSuccess: 登录成功");
        startActivity(MainActivity.class);
    }

    @Override
    public void onUpdate(float progress) {

    }

    @Override
    public void showLoading() {
        circleProgressDialog = new CircleProgressDialog(this);
        circleProgressDialog.setText("登录中...");
        circleProgressDialog.setCancelable(true);
        circleProgressDialog.setDialogSize(120);
        circleProgressDialog.setProgressWidth(15);
        circleProgressDialog.setProgressColor(Color.GRAY);
        circleProgressDialog.setTextColor(Color.WHITE);
        circleProgressDialog.showDialog();
    }

    @Override
    public void hideLoading() {
        circleProgressDialog.dismiss();
    }

    @Override
    public LoginVo getLoginVo() {
        LoginVo loginVo = new LoginVo();
        loginVo.setLoginId(mDropDownEditText.getEditText().getText().toString().trim());
        loginVo.setPassword(mPasswordEditText.getText().trim());
        loginVo.setLastModifyTime(mUserInfoLastModifiedTime);
        return loginVo;
    }
}